<?php
/**
  * tframe
  *
  *
  * @copyright Copyright (c) 2012 - 2014, huxiaomin
  * @author huxiaomin    2012-6-12  
  * @since		Version 1.0
  *
  */

class ACacheApp{

	function hook($name){
	   if ($name == 'refresh'){
	       return array('ajax'=>1);
	   }
	}
	
    function index(){
    	$dplugin = new TTable('dcache_key');
    	$dplugin->cache = 0;
        $tables = $dplugin->get('*');
        $desc = $dplugin->getDesc();
        return $data =array('tables'=>$tables,'fields'=>$desc[1]);//使用模板用此方法
    }
    
    function refresh($request){
    	$name = trim($request['name']);
    	$flag = 0;
    	$data = array();
    	$msg = '';
    	$TMemcache = new TMemcache();
    	switch ($name){
    		case 'table_cache'://表缓存
    			$table = new TTable();
    			$sql = "show tables";
    			$plan = $table->GetCol($sql);
    			foreach ((array)$plan as $table_name){
    			  $sql = "show full fields from {$table_name}";
    			  $gCacheKey = "TTable.cls.php:TTable.getDesc?".$sql;
    			  //echo $gCacheKey;
    			  $TMemcache->delete($gCacheKey);
    			}
    			$flag = 1;
    			break;
    		case 'file_cache'://文件目录缓存
    			 $gCacheKey = "TAutoload.cls.php:TAutoload.getFolder?".TFRAME_DIR;
    			 $TMemcache->delete($gCacheKey);
    			 $gCacheKey = "TMod.cls.php:TMod.getFolder?".TMOD_DIR;
    			 $TMemcache->delete($gCacheKey);
    			 $gCacheKey = "TTable.cls.php:TTable.getFolder?".TTABLE_DIR;
    			 $TMemcache->delete($gCacheKey);
    			 $gCacheKey = "TPlugin.cls.php:TPlugin.getFolder?".TTABLE_DIR;
                 $TMemcache->delete($gCacheKey);
    			 $flag = 1;
    			break;
    		case 'table_config_cache'://查看表文件缓存
    			 $TTableMemcache = new TTableMemcache();
          		 $cache = $TTableMemcache->getConfig();
          		 $flag = 1;
          		 $msg = print_r($cache,1);
    			break;
    		default:
    			 $msg = '禁止hack';
    			break;
    	
    	}
        return goutJson(1,$data,$msg,$request);
    }
    
    /**
     * 升级版本号
     * @param $request
     */
    function refreshCache($request){
        $id = intval($request['id']);
        $dcache_key = new TTable('dcache_key');
        $fileds = array(
                       array('id', $id), 
                    );
        $version = $dcache_key->get('version,gcache_key',$fileds);
        $v = $version[0]['version'] + 1;
        $fileds = array(
                        'id'=>$id,
                        'version'=>$v,
                        'update_time'=>date('Y-m-d H:i:s'),  
                    );
        $dcache_key->update($fileds);
        if ($id == 1){
        	$key = 'SELECT version FROM dcache_key WHERE id=1';
        }else {
        	$key = 'SELECT version FROM dcache_key WHERE gcache_key='."'{$version[0]['gcache_key']}'";	
        }
        $TMemcache = new TMemcache();
        $TMemcache->set($key,$v,0,86400);
        TLog::logDb($request,__CLASS__,__FUNCTION__,'sys');
        return goutJson(1);
                    
    }


}